/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package com.teamreinforce.web.controllers;

import com.teamreinforce.ejb.beans.CollaborateurFacade;
import com.teamreinforce.ejb.beans.ProjetFacade;
import com.teamreinforce.ejb.entities.Collaborateur;
import com.teamreinforce.ejb.entities.Projet;
import com.teamreinforce.ejb.model.IPerformeCollaborator;
import com.teamreinforce.ejb.model.IPerformeProjects;
import java.io.IOException;
import java.io.PrintWriter;
import javax.ejb.EJB;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

/**
 *
 * @author tidiane
 */
@WebServlet(
                name = "CollaboratorControllerServlet",
                loadOnStartup = 1,
                urlPatterns = 
                                {
                                    "/creer-collaborateur",
                                    "/affecter-collaborateur",
                                    "/supprimer",
                                    "/modifier"
                                }
            )
public class CollaboratorControllerServlet extends HttpServlet {

    @EJB
    private ProjetFacade projetF;
    
    @EJB
    private CollaborateurFacade collaboratorF;
    
    @EJB
    private IPerformeProjects pProjets;
    
    @EJB
    private IPerformeCollaborator pCollaborateur;
    
    /**
     * Processes requests for both HTTP
     * <code>GET</code> and
     * <code>POST</code> methods.
     *
     * @param request servlet request
     * @param response servlet response
     * @throws ServletException if a servlet-specific error occurs
     * @throws IOException if an I/O error occurs
     */
    protected void processRequest(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        response.setContentType("text/html;charset=UTF-8");
        PrintWriter out = response.getWriter();
        
        String code = request.getAttribute("code").toString();
        String message = request.getAttribute("message").toString();

        try {

            out.println("<html>");
            out.println("<head>");
            out.println("<link rel =\"stylesheet\" type =\"text/css\" href = \"libs/bootstrap/css/bootstrap.css\" />");
            out.println("<title>Message</title>");            
            out.println("</head>");
            out.println("<body>");
            if ( ! code.equals("error"))
                out.println("<div class=\"alert alert-success\" style = \"padding-top : 60px;\">");
            else
                out.println("<div class=\"alert alert-error\" style = \"padding-top : 60px;\">");
            out.println("<h4 class=\"alert-heading\">" + code +"</h4>");
            out.println(message);
            out.println("</div>");
            out.println("</body>");
            out.println("</html>");
        } 
        finally {            
            out.close();
        }
    }

    // <editor-fold defaultstate="collapsed" desc="HttpServlet methods. Click on the + sign on the left to edit the code.">
    /**
     * Handles the HTTP
     * <code>GET</code> method.
     *
     * @param request servlet request
     * @param response servlet response
     * @throws ServletException if a servlet-specific error occurs
     * @throws IOException if an I/O error occurs
     */
    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        processRequest(request, response);
    }

    /**
     * Handles the HTTP
     * <code>POST</code> method.
     *
     * @param request servlet request
     * @param response servlet response
     * @throws ServletException if a servlet-specific error occurs
     * @throws IOException if an I/O error occurs
     */
    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        request.setCharacterEncoding("UTF-8");
        String url = request.getServletPath();
        /**
         * Creating a collaborateur
         */
        if (url.equals("/creer-collaborateur"))
        {
            System.out.println(" Ici la dans !!");
           try
           {
               String prenom = request.getParameter("prenom");
               String nom = request.getParameter("nom");
               String telephone = request.getParameter("telephone");
               String portable = request.getParameter("portable");
               String email = request.getParameter("email");
               String login = request.getParameter("login");
               String password = request.getParameter("password");
               String est_admin = request.getParameter("est_admin");

               if (est_admin != null && est_admin.equals("on"))
                    pCollaborateur.createCollaborateur(prenom, nom, telephone, portable, email, login, password, true);
               else
                    pCollaborateur.createCollaborateur(prenom, nom, telephone, portable, email, login, password, true);
                request.setAttribute("message", "Collaborateur ajouté avec succès !");
                request.setAttribute("code", "Confirmation");
                
             }catch (Exception e){
                request.setAttribute("message", e.getMessage());
                request.setAttribute("code", "error");
                processRequest(request, response);
             }
           
           processRequest(request, response);
        }
        
        /**
         * Affectations management
         */
        if ( url.equals("/affecter-collaborateur")) {
            System.out.println( " on est bien ici !! ");
            try {
                String pid = request.getParameter("pid");
                String login = request.getParameter("collaborateur");
                String role = request.getParameter("role");

                Projet projet = projetF.find(Integer.parseInt(pid));
                Collaborateur collaborateur = pCollaborateur.getCollaboratorByLogin(login);
                pProjets.addCollaboratorToProject(projet, collaborateur, role);
                
                request.setAttribute("message", "Collaborateur affecté au projet avec succès !");
                request.setAttribute("code", "Confirmation");
            }
            catch (Exception e) {
                request.setAttribute("message", e.getMessage());
                request.setAttribute("code", "error");
                processRequest(request, response);
            }
            processRequest(request, response);
        }
    }

    /**
     * Returns a short description of the servlet.
     *
     * @return a String containing servlet description
     */
    @Override
    public String getServletInfo() {
        return "Short description";
    }// </editor-fold>
}
